package contacts.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import contacts.db.DbConnection;
import contacts.po.Room;
import contacts.po.Student;
import contacts.dao.IContactsDao;

public class StudentDaoImpl implements IContactsDao{

	/**
	 *增加一条通讯录信息
	 */
	@Override
	public void save(Student student) {
		Connection conn = DbConnection.getConnection();
		String sql = "insert into student values(?,?,?,?,?,?,?)";
		PreparedStatement ps = null;
		try {
			ps  = conn.prepareStatement(sql);
			ps.setString(1, student.getStudentId());
			ps.setString(2, student.getStudentName());
			ps.setInt(3, student.getRoom().getRoomId());
			ps.setDate(4, java.sql.Date.valueOf(student.getBirthday()));
			ps.setString(5, student.getPhone());
			ps.setString(6, student.getEmail());
			ps.setString(7, student.getAddress());
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			DbConnection.close(ps, null);
		}
		
	}

	/**
	 *修改通讯录中某一学号的学生的信息
	 */
	@Override
	public void update(Student student) {
		Connection conn = DbConnection.getConnection();	
		String sql = "update student set student_name=?,student_room_id=?,birthday=?,phone=?,email=?,address=? where student_id=?";
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1,student.getStudentName());
			ps.setInt(2,student.getRoom().getRoomId());
			ps.setDate(3,java.sql.Date.valueOf(student.getBirthday()));
			ps.setString(4, student.getPhone());
			ps.setString(5, student.getEmail());
			ps.setString(6, student.getAddress());
			ps.setString(7, student.getStudentId());
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbConnection.close(ps, null);
		}
		
		
	}

	/**
	 *查询一个学生的信息
	 */
	@Override
	public Student get(String Id) {
		Connection conn = null;
		ResultSet rs = null;
		PreparedStatement ps = null;
		conn = DbConnection.getConnection();
		String sql = "select * from student where student_id = ?";
		Student student = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, Id);
			rs = ps.executeQuery();
			while (rs.next()) {
				Room room =new Room(rs.getInt(3),null);
				String studentId= rs.getString(1);
				String studentName= rs.getString(2);
				String birthday=rs.getString(4);
				String phone=rs.getString(5);
				String email=rs.getString(6);
				String address=rs.getString(7);
				student = new Student(studentId,studentName,birthday,phone,email,address,room);
			}
		} catch (SQLException e) {
			System.out.println("Jdbc程序发生错误：" + e);
		} finally {
			DbConnection.close(ps, rs);
		}
		return student;
	}

	/**
	 *删除一个学生的信息
	 */
	@Override
	public void delete(String Id) {
		Connection conn = null;
		PreparedStatement ps = null;
		conn = DbConnection.getConnection();
		String sql = "delete from student where student_id = ?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, Id);
			ps.executeUpdate();
		} catch (SQLException e) {
			System.out.println("Jdbc程序发生错误：" + e);
		} finally {
			DbConnection.close(ps, null);
		}
	}

	/**
	 *查询全部学生的信息
	 */
	@Override
	public List<Student> findAll() {
		Connection conn = null;
		ResultSet rs = null;
		PreparedStatement ps = null;
		conn = DbConnection.getConnection();
		String sql = "select * from student";
		Student student = null;
		List<Student> studentList = new ArrayList<Student>();
		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				Room room =new Room(rs.getInt(3));
				String studentId= rs.getString(1);
				String studentName= rs.getString(2);
				String birthday=rs.getString(4);
				String phone=rs.getString(5);
				String email=rs.getString(6);
				String address=rs.getString(7);
				student = new Student(studentId,studentName,birthday,phone,email,address,room);
				studentList.add(student);
			}
		} catch (SQLException e) {
			System.out.println("Jdbc程序发生错误：" + e);
		} finally {
			DbConnection.close(ps, rs);
		}
		return studentList;
	}

	@Override
	public String getRoomNo(Room room) {
		return null;

	}

	@Override
	public String getMessage(Student student, int i) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int getRoomId(Room room) {
		// TODO Auto-generated method stub
		return 0;
	}

}
